package com.example.service.impl;

import com.example.domain.po.Orders;
import com.example.domain.vo.OrderVo;
import com.example.mapper.OrdersMapper;
import com.example.service.OrdersService;
import com.example.util.PageBean;
import com.example.util.RedisIdWorker;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class OrdersServiceImpl implements OrdersService {


    @Autowired
    private OrdersMapper ordersMapper;

    @Autowired
    private RedisIdWorker redisIdWorker;

    @Override
    public PageBean<OrderVo> list(Integer pageNum, Integer pageSize, Integer status, String startTime, String endTime, Integer userId) {
        //1.创建PageBean对象
        PageBean<OrderVo> pb = new PageBean<>();
        //2.开启分页查询
        PageHelper.startPage(pageNum, pageSize);
        List<OrderVo> ordersList = ordersMapper.list(userId, status, startTime, endTime);

        ordersList.forEach(ordersVo -> {
            if (ordersVo.getCourseStatus().equals(0))  ordersVo.setListed("上架中");
            if (ordersVo.getCourseStatus().equals(1))  ordersVo.setListed("已下架");
        });

        Page<OrderVo> p = (Page<OrderVo>) ordersList;
        pb.setTotal(p.getTotal());
        pb.setItems(p.getResult());
        return pb;
    }

    @Override
    public Orders findById(Integer id) {
        return ordersMapper.findById(id);
    }

    @Override
    public void cancelOrder(Orders order) {
        order.setStatus(3);
         ordersMapper.cancelOrder(order);
    }

    @Override
    public Orders selectByOrderNo(String orderNo) {
        return ordersMapper.selectByOrderNo(orderNo);
    }

    @Override
    public void updateById(Orders orders) {
        System.out.println(orders);
        ordersMapper.updateById(orders);
    }

    @Override
    public Orders getByUidAndCid(Integer cId, Integer uId) {
        return ordersMapper.getByUidAndCid(cId, uId);
    }

    @Override
    public Long add(Orders orders) {
        long z = redisIdWorker.nextId("z");
        orders.setOrderNo(z);
        ordersMapper.add(orders);
        return z;
    }


  /*  @Override
    public void cancelExpiredOrders() {
        //根据未支付状态1查询订单列表
        List<Orders> oList = ordersMapper.listExpiredByStatus(1);
        //改为已过期
        ordersMapper.cancelOrderList(oList);
    }
*/


    @Override
    public List<Orders> findUnpaidOrdersOver30Minutes() {
        Integer time = 2;
        Integer status = 1;
        return ordersMapper.findUnpaidOrdersOver30Minutes(2, 1);
    }



    @Override
    public Orders getByOrderNo(Long orderNo) {
        return ordersMapper.getByOrderNo(orderNo);
    }
}
